package org.sang.service;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.sang.controller.form.ExamCategoryForm;
import org.sang.mapper.ExamCategoryMapper;
import org.sang.pojo.RespBean;
import org.sang.pojo.dto.ExamCategoryDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ExamCategoryServiceImpl implements ExamCategoryService {
    @Resource
    private ExamCategoryMapper examCategoryMapper;

    private final static Logger logger = LoggerFactory.getLogger(ExamCategoryServiceImpl.class);
    @Override
    public RespBean<Integer> addCategory(ExamCategoryDTO examCategory) {
        logger.info("examCategory={}", JSON.toJSONString(examCategory));
        RespBean<Integer> respBean = new RespBean<>();
        if(StringUtils.isEmpty(examCategory.getCateName())){
            respBean.setStatus(400);
            respBean.setMsg("cateName cannot be null");
            return respBean;
        }
        if(StringUtils.isEmpty(examCategory.getCateIcon())){
            respBean.setStatus(400);
            respBean.setMsg("cateIcon cannot be null");
            return respBean;
        }
        if(StringUtils.isEmpty(examCategory.getCateClass())){
            examCategory.setCateClass("其他面试题");
        }
        if(examCategory.getCateOrder()==null){
            examCategory.setCateOrder(0);
        }
        int count = examCategoryMapper.addExamCategory(examCategory);
        if(count==1){
            respBean.setStatus(200);
            respBean.setMsg("success");
            respBean.setData(count);
        } else {
            respBean.setStatus(500);
            respBean.setMsg("failed");
            respBean.setData(0);
        }
        return respBean;
    }

    @Override
    public RespBean<Integer> editCategory(ExamCategoryDTO examCategory) {
        logger.info("editCategory_examCategory={}", JSON.toJSONString(examCategory));
        RespBean<Integer> respBean = new RespBean<>();
        if(examCategory.getId()==null){
            respBean.setStatus(400);
            respBean.setMsg("id cannot be null when update exam_category record");
            return respBean;
        }
        int count = examCategoryMapper.editCategory(examCategory);
        respBean.setStatus(200);
        respBean.setData(count);
        respBean.setMsg("success");
        return respBean;
    }

    @Override
    public RespBean<Integer> deleteById(Integer id) {
        logger.info("deleteById id={}", id);
        RespBean<Integer> respBean = new RespBean<>();
        if(id==null){
            respBean.setStatus(400);
            respBean.setMsg("id cannot be null when delete exam_category record");
            return respBean;
        }
        int count = examCategoryMapper.deleteById(id);
        respBean.setStatus(200);
        respBean.setMsg("success");
        respBean.setData(count);
        return respBean;
    }

    @Override
    public RespBean<List<ExamCategoryDTO>> getAllCategories() {
        List<ExamCategoryDTO> categoryDTOList = examCategoryMapper.getAllCategories();
        RespBean<List<ExamCategoryDTO>> respBean = new RespBean<>();
        respBean.setStatus(200);
        respBean.setMsg("success");
        respBean.setData(categoryDTOList);
        return respBean;
    }

    @Override
    public RespBean<List<ExamCategoryDTO>> findExamCategoriesByCondition(ExamCategoryForm form) {
        logger.info("form={}", JSON.toJSONString(form));
        List<ExamCategoryDTO> categoryDTOList = examCategoryMapper.findCategoriesByCondition(form);
        RespBean<List<ExamCategoryDTO>> respBean = new RespBean<>();
        respBean.setStatus(200);
        respBean.setMsg("success");
        respBean.setData(categoryDTOList);
        return respBean;
    }
}
